package com.tencent.shadow.sample.host.repository.manager;

import android.util.ArrayMap;

import androidx.lifecycle.MutableLiveData;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.meichis.mcsappframework.utils.MessageTools;
import com.meichis.mcsappframework.utils.SharePreferenceUtil;
import com.tencent.shadow.sample.host.MCApplication;
import com.tencent.shadow.sample.host.common.DataCenter;
import com.tencent.shadow.sample.host.http.CallSubscriber;
import com.tencent.shadow.sample.host.http.IWebServiceCallback;
import com.tencent.shadow.sample.host.http.RetrofitUtil.RxSchedulersHelper;
import com.tencent.shadow.sample.host.http.TestCallSubscriber;
import com.tencent.shadow.sample.host.model.DSPToken;
import com.tencent.shadow.sample.host.model.ExtParams;
import com.tencent.shadow.sample.host.model.RotateAdv;
import com.tencent.shadow.sample.host.model.UserInfo;
import com.tencent.shadow.sample.host.repository.ApiUtil;

/**
 * @author szw
 * @version 1.0
 * @date 2020/8/27 14:25
 */
public class LoginRequestManager extends BaseManager {

    private static LoginRequestManager INSTANCE;

    public static LoginRequestManager getInstance() {
        if (INSTANCE == null) {
            synchronized (LoginRequestManager.class) {
                if (INSTANCE == null) {
                    INSTANCE = new LoginRequestManager();
                }
            }
        }
        return INSTANCE;
    }

    public void clearInstance() {
        INSTANCE = null;
    }

    /**
     * 首页轮播图
     *
     * @param showDialog
     */
    public void GetRotateAdvList(final MutableLiveData<Boolean> showDialog, final MutableLiveData<RotateAdv> data) {
        showDialog.setValue(true);
        ArrayMap<String, Object> hs = new ArrayMap<>();
        hs.put("AdvPageCode", DataCenter.AdvPageCode);

        getRootApi().GetRotateAdvList(getToken(), ApiUtil.getRequestPack(hs))
                .compose(RxSchedulersHelper.rxSchedulerHelper())
                .subscribe(new CallSubscriber(new TypeToken<RotateAdv>() {
                }.getType(), new IWebServiceCallback<RotateAdv>() {
                    @Override
                    public void onSucceed(int sequence, RotateAdv resultPack, String ReturnInfo, int Return) {
                        data.setValue(resultPack);
                        showDialog.setValue(false);
                    }

                    @Override
                    public void onFailure(int sequence, int errorCode, String error) {
                        showDialog.setValue(false);
                        MessageTools.showLongToast(error);
                    }
                }));

    }


    /**
     * 用户登陆接口
     *
     * @param showDialog
     * @param appVersion
     * @param appCode
     * @param netType
     * @param userName
     * @param passWord
     * @param data
     */
    public void Login(final MutableLiveData<Boolean> showDialog, String appVersion, String appCode, String netType,
                      String userName, String passWord, final MutableLiveData<DSPToken> data) {

        showDialog.setValue(true);
        ExtParams extParams = new ExtParams();
        extParams.setAppVersion(appVersion);
        extParams.setDeviceMOdel(android.os.Build.MODEL);
        extParams.setAppCode(appCode);
        extParams.setOSVersion(android.os.Build.VERSION.RELEASE);
        extParams.setNetworkType(netType);
//        extParams.setDeviceCode(MCApplication.DeviceID);
//        extParams.setDeviceIds(Tools.getDeviceIds(MCApplication.getContext()));
//        extParams.setGuid(Tools.getDeviceGuid(MCApplication.getContext()));
//        String[] Account = getView().getAccount();
        String DeviceCode = MCApplication.DeviceID;
        String extJson = new Gson().toJson(extParams);
        //将登录设备信息存本地，刷新token时需传
        SharePreferenceUtil.getInstance().setStringValue(SharePreferenceUtil.ExtParams, extJson);

        getRootApi().getToken("password", userName, passWord, "app", "app")
                .compose(RxSchedulersHelper.rxSchedulerHelper()).subscribe(new CallSubscriber(new TypeToken<DSPToken>() {
                }.getType(), new IWebServiceCallback<DSPToken>() {
                    @Override
                    public void onSucceed(int sequence, DSPToken resultPack, String retueninfo, int Return) {

                        showDialog.setValue(false);
                        data.setValue(resultPack);
                    }

                    @Override
                    public void onFailure(int sequence, int errorCode, String error) {
                        showDialog.setValue(false);
                        MessageTools.showLongToast(error);
                    }
                }));
    }

    /**
     * 用户登陆接口
     *
     * @param showDialog
     * @param appVersion
     * @param appCode
     * @param netType
     * @param userName
     * @param passWord
     * @param data
     */
    public void LoginTest(final MutableLiveData<Boolean> showDialog, String appVersion, String appCode, String netType,
                          String userName, String passWord, final MutableLiveData<DSPToken> data, final MutableLiveData<String> errorData) {

        showDialog.setValue(true);
        ExtParams extParams = new ExtParams();
        extParams.setAppVersion(appVersion);
        extParams.setDeviceMOdel(android.os.Build.MODEL);
        extParams.setAppCode(appCode);
        extParams.setOSVersion(android.os.Build.VERSION.RELEASE);
        extParams.setNetworkType(netType);
//        extParams.setDeviceCode(MCApplication.DeviceID);
//        extParams.setDeviceIds(Tools.getDeviceIds(MCApplication.getContext()));
//        extParams.setGuid(Tools.getDeviceGuid(MCApplication.getContext()));
//        String[] Account = getView().getAccount();
        String DeviceCode = MCApplication.DeviceID;
        String extJson = new Gson().toJson(extParams);
        //将登录设备信息存本地，刷新token时需传
        SharePreferenceUtil.getInstance().setStringValue(SharePreferenceUtil.ExtParams, extJson);

        getRootApi().getToken("password", userName, passWord, "app", "app")
                .compose(RxSchedulersHelper.rxSchedulerHelper()).subscribe(new TestCallSubscriber(new TypeToken<DSPToken>() {
                }.getType(), new IWebServiceCallback<DSPToken>() {
                    @Override
                    public void onSucceed(int sequence, DSPToken resultPack, String retueninfo, int Return) {

                        showDialog.setValue(false);
                        data.setValue(resultPack);
                    }

                    @Override
                    public void onFailure(int sequence, int errorCode, String error) {
                        showDialog.setValue(false);
                        MessageTools.showLongToast(error);
                        errorData.setValue(error);
                    }
                }));
    }


    /**
     * 获取登陆人信息
     *
     * @param showDialog
     */
    public void GetCurrentUser(final MutableLiveData<Boolean> showDialog, MutableLiveData<UserInfo> data) {
        showDialog.setValue(true);

        getRootApi().getCurrentUser(getToken()).compose(RxSchedulersHelper.rxSchedulerHelper()).subscribe(new CallSubscriber(new TypeToken<UserInfo>() {
        }.getType(), new IWebServiceCallback<UserInfo>() {
            @Override
            public void onSucceed(int sequence, UserInfo resultPack, String ReturnInfo, int Return) {
                showDialog.setValue(false);
                data.setValue(resultPack);
            }

            @Override
            public void onFailure(int sequence, int errorCode, String error) {
                showDialog.setValue(false);
                MessageTools.showLongToast(error);
            }
        }));
    }
}
